#REQUIRES -pssnapin PoshXmpp
## http://CodePlex.com/PoshXmpp
## Don't forget to: InstallUtil PoshXmpp.dll; Add-PSSNapin PoshXmpp;
## ALSO REQUIRES Import-BufferBox http://powershellcentral.com/scripts/110
##
PARAM (
    $JabberId = $( Read-Host "Bot's Jabber ID" )
   ,$Password = $( Read-Host "Bot's Password" -asSecure)
   ,$Chat = "PowerShell%irc.FreeNode.net@irc.im.flosoft.biz"      # A Chat room to join!
   ,$Nickname = $("PowerBot$((new-object Random).Next(0,9999))")  # Your nickname in IRC
   ,$LogFile = "ChatLog.txt"
)
## Requires Import-BufferBox http://powershellcentral.com/scripts/110
$e = $ErrorActionPreference
$ErrorActionPreference = "Stop"
. .\Import-BufferBox.ps1
$ErrorActionPreference = $e

###################################################################################################
##### Jabber Chat functions
###################################################################################################
function PoshXmppReceiveAll( )
{
   PoshXmpp\Receive-Message -All | foreach-object {
      Write-Message ("{0}-{1} said: {2}" -f ($_.From.User.split("%",2))[0], $_.From.Resource, $_.Body)
   }
}

function PoshXmppProcessLine($line)
{
   if(![string]::IsNullOrEmpty($line)){
      switch -regex ($line) {
         "^/quit.*" {
            $script:stay = $false
         }
         "^/nick.*" {
            $tokens = $line.split(" ",3)
            if($tokens.length -gt 2) { $Nickname = $tokens[1] }
            PoshXmpp\Connect-Chat $Chat $Nickname
         }
         default {
            PoshXmpp\Send-Message $Chat $line
         }
      }
   }
}

function Start-ChatConnection {

   Write-Message "Welcome to PowerXmpp Chat - courtesy of Joel Bennett"
   Write-Message "You're about to join the $Chat Multi-User chat room"
   Write-Message "You can type /list to get a user list, or type /quit to exit chat."
   Write-Message "There are a few other commands, type /help to get a list of them."

   $line=""
   $script:stay = $true
   while($script:stay)
   {
      PoshXmppReceiveAll
      ## TODO: figure out how to tell if there's a whole LINE available...
      while($Host.UI.RawUI.KeyAvailable) {
         $k = $Host.UI.RawUI.ReadKey( "IncludeKeyUp" )
         if($k.VirtualKeyCode -eq 13 ) {
            PoshXmppProcessLine $line
            $line=""
            Clear-Prompt
         }
         elseif($k.Character -ne 0) 
         {
            $line+=$k.Character
         }

      }
      [System.Threading.Thread]::Sleep(100)
   }
   Write-Message "Disconnecting ..."
   Disconnect-Chat $Chat $Nickname
   [System.Threading.Thread]::Sleep(1000)
   
   $Host.UI.RawUI.ForegroundColor = $fore
   $Host.UI.RawUI.BackgroundColor = $back
}

## Initialize The XMPP connection before we clear the buffer
$global:PoshXmppClient = PoshXmpp\New-Client $JabberId $Password # http://im.flosoft.biz:5280/http-poll/ >> $LogFile
PoshXmpp\Connect-Chat $Chat $Nickname

Initialize-BufferBox @($Chat.Split("@"))[0]
Start-ChatConnection
